package com.example.sinoyd.frameapplication.ProjectCodes.Fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.sinoyd.frameapplication.KotlinFrame.Dataclass.GVkeytime
import com.example.sinoyd.frameapplication.KotlinFrame.Dataclass.GVkeyvalue
import com.example.sinoyd.frameapplication.KotlinFrame.Dataclass.gson
import com.example.sinoyd.frameapplication.KotlinFrame.Frame.Uitl.Networkrequestmodel
import com.example.sinoyd.frameapplication.KotlinFrame.Uitl.showdialog
import com.example.sinoyd.frameapplication.KotlinFrame.View.TimeSelectionBarView
import com.example.sinoyd.frameapplication.ProjectCodes.Dataclass.GetAlarmInfo
import com.example.sinoyd.frameapplication.R
import com.sinoyd.Code.Until.Networkrequestaddress
import com.sinoyd.Code.Until.SharedPreferencesFactory
import com.sinoyd.environmentsz.Kotlin.getToday
import com.sinoyd.environmentsz.Kotlin.getlastweekToday
import kotlinx.android.synthetic.main.alarm_information_fragment.*
import kotlinx.android.synthetic.main.include_title_layout.*
import okhttp3.Response
import org.jetbrains.anko.act
import org.jetbrains.anko.runOnUiThread
import java.util.*

/**
 * 报警信息界面
 * **/
@SuppressLint("ValidFragment")
class AlarmInformationFragment(var name: String = "报警信息") : BaseFragment() {
    var date = Date()
    var startTime = ""
    var endTime = ""
    //选择站点后进行的操作
    override fun selectPortCallBack() {
        getAlarmInfo()
        iv_left_two.text = portname
    }


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val conview = inflater.inflate(R.layout.alarm_information_fragment, null)
        return conview
    }

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setview()
        setlisterens()
        getAlarmInfo()
    }


    //设置界面
    private fun setview() {
        center_title.text = name
        startTime = date.getlastweekToday()
        endTime = date.getToday()
    }

    //设置各种监听
    private fun setlisterens() {
        //时间选择器监听
        timeselection.setdefaulttime(date.getlastweekToday(), date.getToday())
                .setdefaulttextcolor("#000000")
                .setfaultimagecolor("#000000")
                .setTimeSelectionbarlistener(object : TimeSelectionBarView.TimeSelectionbarlistener {
                    override fun timeselectionbardoing(lefttime: String, righttime: String) {
                        startTime = lefttime
                        endTime = righttime
                        getAlarmInfo()
                    }
                }).show()
    }

    //获取警告信息
    fun getAlarmInfo() {
        showdialog(act, "loadshow")
        var ntrequest = Networkrequestmodel()
        ntrequest.setMethod(Networkrequestmodel.GETREQUEST)
                .settag("GetAlarmInfo")
                .seturl(Networkrequestaddress.URL_GetAlarmInfo)
                .addparam("portId", portid)
                .addparam("startTime", startTime)
                .addparam("endTime", endTime)
                .addparam("sysType", SharedPreferencesFactory.getdata(act, "OS"))
                .start(this)
    }

    var getAlarmInfo = GetAlarmInfo()
    override fun requestSuccess(response: Response, TAG: String) {
        super.requestSuccess(response, TAG)
        when (TAG) {
            "GetAlarmInfo" -> {
                getAlarmInfo = gson.fromJson(responsestr, GetAlarmInfo::class.java)

                if (getAlarmInfo.alarmInfo == null || getAlarmInfo.alarmInfo.size == 0) {
                    runOnUiThread {
                        tv_message.visibility = View.VISIBLE
                        twodimensionallistview.visibility = View.GONE
                    }
                    //                    Looper.prepare()
//                    toast("未获取到报警信息")
//                    Looper.loop()
                    var list: ArrayList<GVkeyvalue> = ArrayList()
                    runOnUiThread {
                        twodimensionallistview.initview("点位", false, false).setdata(list).show()
                    }
                } else {
                    runOnUiThread {
                        tv_message.visibility = View.GONE
                        twodimensionallistview.visibility = View.VISIBLE
                    }

                    var list: ArrayList<GVkeyvalue> = ArrayList()
                    var textid = ""
                    for (item in getAlarmInfo.alarmInfo) {
                        textid = UUID.randomUUID().toString()
                        list.add(GVkeyvalue(GVkeytime(item.portName, textid), GVkeytime("报警时间", ""), item.alarmTime))
                        list.add(GVkeyvalue(GVkeytime(item.portName, textid), GVkeytime("报警类型", ""), item.alarmType))
                        list.add(GVkeyvalue(GVkeytime(item.portName, textid), GVkeytime("报警内容", ""), item.alarmContent))
                        list.add(GVkeyvalue(GVkeytime(item.portName, textid), GVkeytime("报警级别", ""), item.alarmGrade))
                        list.add(GVkeyvalue(GVkeytime(item.portName, textid), GVkeytime("报警结果", ""), item.alarmProcess))
                    }
                    //显示二维图表
                    Log.i("scj", "显示的数据条数" + list.size)
                    runOnUiThread {
                        twodimensionallistview.initview("点位", false, false).setdata(list).show()
                    }
                }

            }
        }
    }
}